package com.kemai.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.kemai.common.ui.BaseActivity
import com.kemai.customize.ColorTraceTextView

//自定义TextView
class CustomizeTextViewActivity:BaseActivity(){
    private lateinit var  bt_left_to_right: Button
    private lateinit var  bt_right_to_left:Button
    private lateinit var  colorTraceTextView: ColorTraceTextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customize_textview)
        setTiTle("文字变色")
    }

    override fun findViewById() {
        super.findViewById()
        bt_left_to_right = findViewById(R.id.bt_left_to_right)
        bt_right_to_left = findViewById(R.id.bt_right_to_left)
        colorTraceTextView = findViewById(R.id.colorTraceTextView)
        colorTraceTextView.setDirection(ColorTraceTextView.Direction.LEFT_TO_RIGHT)
        setOnClickListener()
    }

    override fun setOnClickListener(){
        bt_left_to_right.setOnClickListener(this)
        bt_right_to_left.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.bt_left_to_right->{

                setAnimation(ColorTraceTextView.Direction.LEFT_TO_RIGHT)

            }
            R.id.bt_right_to_left->{

                setAnimation(ColorTraceTextView.Direction.RIGHT_TO_LEFT)

            }
        }
    }
    private fun setAnimation(direction: ColorTraceTextView.Direction){
        colorTraceTextView.setDirection(direction)
        val valueAnimator:ValueAnimator = ObjectAnimator.ofFloat(0.0f,1.0f)
        valueAnimator.duration = 2000
        valueAnimator.addUpdateListener(ValueAnimator.AnimatorUpdateListener {
            val currentProgress = it.animatedValue
            colorTraceTextView.setCurrentPoint(currentProgress as Float)
        })
        valueAnimator.start()

    }
}